<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.alipay.api.internal.util.AlipaySignature" %>
<%@ page import="com.example.config.AlipayConfig" %>
<%@ page import="com.alipay.api.AlipayApiException" %>
<%@ page import="com.example.domain.Chapter" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="java.util.*" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>支付结果</title>
    <style>
        .result-container {
            text-align: center;
            margin-top: 50px;
            font-family: Arial, sans-serif;
        }
        .success {
            color: #4CAF50;
            font-size: 24px;
            margin-bottom: 20px;
        }
        .button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            display: inline-block;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="result-container">
    <%
        // 获取支付宝GET过来的信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = iter.next();
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }

        // 验证签名
        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(
                    params,
                    AlipayConfig.alipay_public_key,
                    AlipayConfig.charset,
                    AlipayConfig.sign_type
            );
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

        if(signVerified) {
            // 商户订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
            //            获取小说id
//            String novelId = new String(request.getParameter("novel_id").getBytes("ISO-8859-1"),"UTF-8");
            // 获取章节ID（从订单号中提取）
            String chapterId = out_trade_no.split("-")[1].split("#")[0];
            String novelId = out_trade_no.split("#")[1];

//            获取章节列表
//            List<Chapter> chapterList= (List<Chapter>) request.getSession().getAttribute("chapterList");
//
//            session.setAttribute("chapterList",chapterList);
    %>
    <div class="success">支付成功！</div>
    <p>订单号: <%= out_trade_no %></p>
    <p>支付宝交易号: <%= trade_no %></p>
<%--    <%--%>
<%--        List<Chapter> chapters = (List<Chapter>) request.getSession().getAttribute("chapterList");--%>
<%--        System.out.println(chapters);--%>
<%--    %>--%>
    <script>
        // 调用父窗口的支付成功处理函数
        if (window.opener && !window.opener.closed) {
            window.opener.onPaymentSuccess('<%= chapterId %>','<%= novelId %>');
            setTimeout(function() {
                window.close();
            }, 2000);
        } else {
            // 如果不是弹窗，则直接跳转
            setTimeout(function() {
                window.location.href = 'NovelContentPayServlet?novel_id=<%= novelId %>&chapter_id=<%= chapterId %>';
            }, 2000);
        }
    </script>

    <a href="NovelContentPayServlet?novel_id=<%= novelId %>&chapter_id=<%= chapterId %>" class="button">
        立即阅读
    </a>
    <%
    } else {
    %>
    <div class="success" style="color: red;">验证失败</div>
    <p>签名验证失败，请联系管理员</p>
    <%
        }
    %>
</div>
</body>
</html>